package main

/*
#cgo LDFLAGS: -lavutil

#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
*/
import "C"

import (
	"flag"
	"log"

	"ffmpeg"
)

var outputFile = flag.String("output", "", "Output file name.")
var duration = flag.Int("duration", 60, "Duration in secs.")

func main() {
	flag.Parse()

	var output *ffmpeg.Output = nil
	if len(*outputFile) > 0 {
		var err error
		if output, err = ffmpeg.Create(*outputFile, ""); err != nil {
			log.Fatalf("Failed to open %s for write: %s!\n", *outputFile, err)
		}

	}

	for _, arg := range flag.Args() {
		if input, err := ffmpeg.Open(arg); err == nil {
			log.Println(input.DebugString())
			for _, stream := range input.Streams {
				log.Println(stream.DebugString())
			}
			if output != nil {
				if err := output.Write(input, 0, *duration); err != nil {
					log.Printf("Failed to write %s: %s!\n", arg, err)
				}
			}
			input.Close()
		} else {
			log.Println(err)
		}
	}
}
